<?php
/**
 * Created by PhpStorm.
 * User: fyl
 * Date: 2016/8/13
 * Time: 11:06
 */

class Involved_CategorySpecModel extends BaseModel{

    protected $table = 'category_spec';

    /**
     * 获取品名列表
     * @author fyl
     * @param $id
     * @date 2016-08-13
     */
    public function GetSpec($id)
    {
        return DB::table($this->table)
            ->select('id','spec_name')
            ->where('fid',$id)
            ->get();
    }
    public function GetCategory(){
        return DB::table($this->table)
            ->select('id','category_name')
            ->get();
    }
    public function SelectCategory($id){
        return DB::table($this->table)
            ->select('category_name')
            ->where('id',$id)
            ->first();
    }
    public function ShowProductCategory(){
        return DB::table($this->table)
            ->select('id','category_name')
            ->where('fid',0)
            ->get();
    }
    public function GetProductCategoryName($id)
    {
        return  DB::table($this->table)
            ->where('id',$id)
            ->first();
    }
    public function ShowProductSpec(){
        return DB::table($this->table)
            ->select('id','spec_name','fid')
            ->where('fid', '!=', 0)
            ->get();

    }
    public function GetProductSpecName($id)
    {
        return  DB::table($this->table)
            ->where('id',$id)
            ->first();
    }


    //-------------------------------------API-------------------------------

    /**
     * 品类信息
     * @return mixed|string
     * @author lzhp
     * @data  2016-09-13
     */
    public function CategoryData()
    {
        $data1 = DB::table($this->table)
            ->select('id','fid', 'category_name')
            ->where('fid',0)
            ->get();

        $data2 = DB::table($this->table)
            ->select('id','fid','spec_name')
            ->where('fid','!=',0)
            ->get();


        $data1 = json_encode($data1);
        $data1 = json_decode($data1,true);
        $data2 = json_encode($data2);
        $data2 = json_decode($data2,true);

        foreach($data1 as $k=>$v){
            foreach($data2 as $key=>$val){
                if($v['id'] == $val['fid']){
                    $data1[$k]['spec_name'][] = $val;
                }
            }
            if(empty($data1[$k]['spec_name'])){
                $data1[$k]['spec_name']= [];
            }
        }


        return $data1;
    }

    /**
     * @author lzhp
     * @data  2016-09-13
     */
    public function HotSearch()
    {
        return DB::table($this->table)
            ->select( 'category_name')
            ->where('id','>','1')
            ->where('id','<','41')
            ->where('fid',0)
            ->get();
    }

}